package com.ruoyi.reportInfo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportInfo.domain.bo.IndustrialStationOperationInfoBo;
import com.ruoyi.reportInfo.domain.vo.IndustrialStationOperationInfoVo;
import com.ruoyi.reportInfo.domain.IndustrialStationOperationInfo;
import com.ruoyi.reportInfo.mapper.IndustrialStationOperationInfoMapper;
import com.ruoyi.reportInfo.service.IIndustrialStationOperationInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 场站物流运营信息Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-24
 */
@RequiredArgsConstructor
@Service
public class IndustrialStationOperationInfoServiceImpl implements IIndustrialStationOperationInfoService {

    private final IndustrialStationOperationInfoMapper baseMapper;

    /**
     * 查询场站物流运营信息
     */
    @Override
    public IndustrialStationOperationInfoVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询场站物流运营信息列表
     */
    @Override
    public TableDataInfo<IndustrialStationOperationInfoVo> queryPageList(IndustrialStationOperationInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialStationOperationInfo> lqw = buildQueryWrapper(bo);
        Page<IndustrialStationOperationInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询场站物流运营信息列表
     */
    @Override
    public List<IndustrialStationOperationInfoVo> queryList(IndustrialStationOperationInfoBo bo) {
        LambdaQueryWrapper<IndustrialStationOperationInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialStationOperationInfo> buildQueryWrapper(IndustrialStationOperationInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialStationOperationInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStationId()), IndustrialStationOperationInfo::getStationId, bo.getStationId());
        lqw.eq(bo.getReportDate() != null, IndustrialStationOperationInfo::getReportDate, bo.getReportDate());
        lqw.eq(bo.getFreightVolume() != null, IndustrialStationOperationInfo::getFreightVolume, bo.getFreightVolume());
        lqw.eq(bo.getCarNum() != null, IndustrialStationOperationInfo::getCarNum, bo.getCarNum());
        lqw.eq(bo.getReportCollectDate() != null, IndustrialStationOperationInfo::getReportCollectDate, bo.getReportCollectDate());
        return lqw;
    }

    /**
     * 新增场站物流运营信息
     */
    @Override
    public Boolean insertByBo(IndustrialStationOperationInfoBo bo) {
        IndustrialStationOperationInfo add = BeanUtil.toBean(bo, IndustrialStationOperationInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改场站物流运营信息
     */
    @Override
    public Boolean updateByBo(IndustrialStationOperationInfoBo bo) {
        IndustrialStationOperationInfo update = BeanUtil.toBean(bo, IndustrialStationOperationInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialStationOperationInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除场站物流运营信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
